package sdu.icat.ltp.api;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import org.apache.log4j.Logger;

/**
 * LtpFactory模式
 * @author ljh_2015
 *
 */
public class LtpAPIConnectionFactory {

    private static final Logger log = Logger.getLogger(LtpAPIConnectionFactory.class);
    private static final int maxSize = 100;
   
    private static ArrayList<LtpAPIConnection> connections;
    private static final Queue<String> request = new LinkedList<>();
    private static final String api_key = "p8D9P0V1rsbV3aBKCudxIVwgpyjb8GjGFXKwHxsV";
    private static final String pattern = "all";
    private static final String format = "xml";
    
    static {
	log.debug("-------- ltp connections init -------");
	if(connections==null) {
	    connections = new ArrayList<>();
	}
	for(int i=0;i<maxSize;i++) {
	    connections.add(new LtpAPIConnection(api_key, null, format, pattern));
	}
	log.debug("-------- ltp connections init successfully -------");
    }
    
    public static LtpAPIConnection buildText(String text) {
	if(connections.isEmpty()) {
	    return null;
	}
	int last = connections.size();
	if(last==0) {
	    try {
		Thread.sleep(1000);
		return buildText(text);
	    } catch (InterruptedException e) {
		e.printStackTrace();
	    }
	}
	LtpAPIConnection connection = connections.remove(last-1);
	connection.setText(text);
	return connection;
    }
    
    public static void closeConnection(LtpAPIConnection api) {
	api.setText(null);
	connections.add(api);
    }
    
}
